﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Home.Haushalt.Lib;

namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UcUserSelectorItem : UserControl
    {
        /// <summary></summary>
        public event EventHandler OnUserClick;

        /// <summary></summary>
        public string UserName { get; private set; }

        /// <summary></summary>
        public bool _selected = false;

        /// <summary></summary>
        private const double dif = 0.8;

        /// <summary></summary>
        private const int _size = 97;


        /// <summary></summary>
        public bool Selected
        {
            get { return _selected; }
            set
            {
                if (value)
                {
                    _selected = true;
                    imgUser.Width = _size;
                    lblUser.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
                }
                else
                {
                    _selected = false;
                    imgUser.Width = (int)(_size * dif);
                    lblUser.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F);
                }

                imgUser.Height = imgUser.Width;
                imgUser.Left = (this.Width - imgUser.Width) / 2;
                lblUser.Top = imgUser.Height + 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="onClick"></param>
        public UcUserSelectorItem(CSettings settings, EventHandler onClick)
        {
            InitializeComponent();

            UserName = settings.UserName;
            OnUserClick = onClick;

            lblUser.Text = UserName;

            string userImageFileName = Directory.GetFiles(settings.DataFullPath, UserName + ".*", SearchOption.TopDirectoryOnly).FirstOrDefault();

            if (userImageFileName != default(string))
            {
                imgUser.Image = Image.FromFile(userImageFileName);
            }
            else
            {
                imgUser.Image =global:: Home.Haushalt.Gui.Properties.Resources.user;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgUser_Click(object sender, EventArgs e)
        {
            if (OnUserClick != null)
                OnUserClick(this, e);
        }


    }
}
